﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Framework.Logic.Web;

namespace Framework.Forms
{
    public partial class BukkitUpdate : Form
    {
        public BukkitUpdate()
        {
            InitializeComponent();
        }

        private void BukkitUpdate_Load(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();

            ListViewItem item = new ListViewItem();
            item.Text = "1.2.5-R1.0";
            item.Name = "http://dl.bukkit.org/downloads/craftbukkit/get/01026_1.2.5-R1.0/craftbukkit.jar";
            listView1.Items.Add(item);

            item = new ListViewItem();
            item.Text = "1.2.4-R1.0";
            item.Name = "http://dl.bukkit.org/downloads/craftbukkit/get/00993_1.2.4-R1.0/craftbukkit.jar";
            listView1.Items.Add(item);

            item = new ListViewItem();
            item.Text = "1.1-R6";
            item.Name = "http://dl.bukkit.org/downloads/craftbukkit/get/00790_1.1-R6/craftbukkit.jar";
            listView1.Items.Add(item);

            item = new ListViewItem();
            item.Text = "1.1-R4";
            item.Name = "http://dl.bukkit.org/downloads/craftbukkit/get/00720_1.1-R4/craftbukkit.jar";
            listView1.Items.Add(item);

            item = new ListViewItem();
            item.Text = "1.1-R3";
            item.Name = "http://dl.bukkit.org/downloads/craftbukkit/get/00705_1.1-R3/craftbukkit.jar";
            listView1.Items.Add(item);

            item = new ListViewItem();
            item.Text = "1.0.1-R1";
            item.Name = "http://dl.bukkit.org/downloads/craftbukkit/get/00252_1.0.1-R1/craftbukkit.jar";
            listView1.Items.Add(item);

        }

        public string Status
        {
            get { return lblStatus.Text; }
            set { lblStatus.Text = value; }
        }

        public bool EnableForm
        {
            set { btnDownload.Enabled = value; listView1.Enabled = value; btnCancel.Enabled = value; if (value) { btnDownload.Enabled = false; } }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select a bukkit version before clicking Download.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                bool setTrue = false;

                if (MessageBox.Show("Do you want to set the default startup server to this bukkit version?", this.Text, MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    setTrue = true;
                }
                EnableForm = false;
                if (!Download.DownloadBukkitServer(saveFileDialog1.FileName, listView1.SelectedItems[0].Name, setTrue, this))
                {
                    MessageBox.Show("An error has occurred. Error code: 112");
                    this.Close();
                    this.Dispose();
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDownload.Enabled = true;
        }
    }
}